package com.tsd.jxc.sale.entity;

import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.goods.entity.JxcGoodsPriceExt;
import com.tsd.jxc.utils.JxcUtils;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.jxc.vo.PrintVo;
import io.swagger.annotations.ApiModel;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 销售报价-明细扩展
 *
 * @author Hillpool
 * @date 2022/12/12 11:26:43
 */
@ApiModel(description = "销售报价-明细扩展")
public class JxcSaleQuotationDetailExt extends JxcSaleQuotationDetail implements Serializable {

    private List<String> quotation_list;

    private String goods_code;

    private String goods_name;

    private Integer unit_qty;

    private List<JxcGoodsPriceExt> price_list;

    public List<String> getQuotation_list() {
        return quotation_list;
    }

    public void setQuotation_list(List<String> quotation_list) {
        this.quotation_list = quotation_list;
    }

    public String getGoods_code() {
        return goods_code;
    }

    public void setGoods_code(String goods_code) {
        this.goods_code = goods_code;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public String getSpec_name() {
        return null;
    }

    public Integer getUnit_qty() {
        return unit_qty;
    }

    public void setUnit_qty(Integer unit_qty) {
        this.unit_qty = unit_qty;
    }

    public List<JxcGoodsPriceExt> getPrice_list() {
        return price_list;
    }

    public void setPrice_list(List<JxcGoodsPriceExt> price_list) {
        this.price_list = price_list;
    }

    public void calcGoodsPrice() {
        List<JxcGoodsPriceExt> priceExtList = this.getPrice_list();
        if (!HlpUtils.isEmptyList(priceExtList)) {
            if (HlpUtils.isEmpty(this.getQty4base())) {
                JxcGoodsPriceExt priceExt = ListUtil.get(priceExtList, t -> t.getUnit_name().equals(this.getUnit()));
                if (priceExt != null) {
                    this.setQty4base(BigDecimal.valueOf(DoubleUtil.mul(this.getQty(), priceExt.getQty())));
                }
            }
            double baseUnitPrice = DoubleUtil.div(this.getAmount(), this.getQty4base(), 4);
            for (JxcGoodsPriceExt priceExt : priceExtList) {
                priceExt.setBuy_price(BigDecimal.valueOf(DoubleUtil.mul(baseUnitPrice, priceExt.getQty())));
            }
            this.setPrice_list(priceExtList);
        }
    }

    public void calcAmount() {
        JxcSettingVo settingVo = JxcUtils.getJxcSettingVo();
        if (HlpUtils.isEmpty(this.getInvoice_rate())) {
            this.setInvoice_rate(settingVo.getDefault_tax_ratio());
        }
        if (HlpUtils.isEmpty(this.getAmount())) {
            double amount = DoubleUtil.mul(this.getQty(), this.getPrice());
            this.setAmount(BigDecimal.valueOf(amount));
        }
        this.setTax_amount(settingVo.getTaxAmount(this.getAmount(), this.getInvoice_rate(), false));
        this.setTotal_amount(BigDecimal.valueOf(DoubleUtil.add(this.getAmount(), this.getTax_amount())));
    }

    public JxcSaleOrderDetailExt genSaleOrderDetail() {
        JxcSaleOrderDetailExt detailExt = new JxcSaleOrderDetailExt();
        detailExt.setGoods_sid(this.getGoods_sid());
        detailExt.setQty(this.getQty());
        detailExt.setList_price(this.getPrice());
        detailExt.setPrice_rate(BigDecimal.ONE);
        detailExt.setPrice(this.getPrice());
        detailExt.setQty4base(this.getQty4base());
        detailExt.setAmount(this.getAmount());
        detailExt.setTax_rate(this.getInvoice_rate());
        detailExt.setTax_amount(this.getTax_amount());
        detailExt.setTotal_amount(this.getTotal_amount());
        detailExt.setSpec_info(this.getSpec_info());
        detailExt.setUnit_name(this.getUnit());
        detailExt.setAvg_cost(BigDecimal.ZERO);
        detailExt.setSn(this.getSn());
        return detailExt;
    }

    public PrintVo convert2Print() {
        PrintVo vo = new PrintVo();
        vo.setSn(this.getSn());
        vo.setGoods_name(this.getGoods_name());
        vo.setSpec_name(this.getSpec_name());
        vo.setQty(this.getQty());
        vo.setPrice(this.getPrice());
        vo.setAmount(this.getAmount());
        vo.setNote_info(this.getNote_info());
        vo.setSpec_info(this.getSpec_info());
        vo.setUnit(this.getUnit());
        return vo;
    }
}
